﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Heyu.Utility
{
    public class EnumUtility
    {

        /// <summary>
        /// 获取应用类型名称
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetName(Type type, int value)
        {
            return type.GetEnumName(value);
        }

        /// <summary>
        /// 通过枚举值获取描述
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDesc(Type type, int value)
        {
            string output = null;
            string enumName = type.GetEnumName(value);

            FieldInfo fi = type.GetField(enumName);
            DescriptionAttribute[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
            if (attrs.Length > 0)
            {
                output = attrs[0].Description;
            }
            return output;
        }

        /// <summary>
        /// 通过枚举值获取描述
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetDesc(Type type, string name)
        {
            string output = null;

            FieldInfo fi = type.GetField(name);
            DescriptionAttribute[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
            if (attrs.Length > 0)
            {
                output = attrs[0].Description;
            }
            return output;
        }

        /// <summary>
        /// 通过描述获取值
        /// </summary>
        /// <param name="type"></param>
        /// <param name="desc"></param>
        /// <returns></returns>
        public static int GetValue(Type type, string desc)
        {
            int value = -1;
            Array arr = type.GetEnumValues();
            foreach (int i in arr)
            {
                string enumDesc = GetDesc(type, i);
                if (enumDesc == desc)
                {
                    value = i;
                    break;
                }
            }
            return value;
        }

        /// <summary>
        /// 获取枚举的描述信息
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDesc(Enum value)
        {
            string output = null;
            Type type = value.GetType();

            FieldInfo fi = type.GetField(value.ToString());
            DescriptionAttribute[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
            if (attrs.Length > 0)
            {
                output = attrs[0].Description;
            }
            return output;
        }


        ///<summary>
        /// 返回 Dic<枚举项，描述>
        ///</summary>
        ///<param name="enumType"></param>
        ///<returns>Dic<枚举项，描述></returns>
        public static Dictionary<string, string> GetEnumDic(Type enumType)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            FieldInfo[] fieldinfos = enumType.GetFields();
            foreach (FieldInfo field in fieldinfos)
            {
                if (field.FieldType.IsEnum)
                {
                    Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);

                    dic.Add(field.Name, ((DescriptionAttribute)objs[0]).Description);
                }

            }

            return dic;
        }

        ///<summary>
        /// 从枚举类型和它的特性读出并返回一个键值对
        ///</summary>
        ///<param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
        ///<returns>键值对</returns>
        public static NameValueCollection GetNVCFromEnumValue(Type enumType)
        {
            NameValueCollection nvc = new NameValueCollection();
            Type typeDescription = typeof(DescriptionAttribute);
            System.Reflection.FieldInfo[] fields = enumType.GetFields();
            string strText = string.Empty;
            string strValue = string.Empty;
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    if (arr.Length > 0)
                    {
                        DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                        strText = aa.Description;
                    }
                    else
                    {
                        strText = field.Name;
                    }
                    nvc.Add(strValue, strText);
                }
            }
            return nvc;
        }


    } 
}
